/**
 * 
 */

$.fn.bootdatagrid = function(url,options){
	var _self = this;
	var baseopt = {
		    url: url
		    ,cache:false
		   // ,method:'post'
		    //,height:600
		    ,striped:true
		    ,pagination:true
		    //,selectItemName:'id'
		    //,showFooter:true
		    ,clickToSelect:true
		    ,sidePagination:'server'
		   // ,icons:{toggle: 'glyphicon-list-alt icon-list-alt'}
		    ,icons:{
		    	  paginationSwitchDown: 'glyphicon-collapse-down icon-chevron-down',
		    	  paginationSwitchUp: 'glyphicon-collapse-up icon-chevron-up',
		    	  refresh: 'glyphicon-refresh icon-refresh',
		    	  toggle: 'glyphicon-th-large icon-list-alt',
		    	  columns: 'glyphicon-th-list icon-th',
		    	  detailOpen: 'glyphicon-plus icon-plus',
		    	  detailClose: 'glyphicon-minus icon-minus',
		    	  export:'glyphicon-floppy-save icon-share'//'glyphicon-export icon-share'
		    }
		    ,pageNumber:1
		    ,pageSize:10
		    ,pageList:'[10, 25, 50, 100, All]'
		    //,exportDataType:'all'
		    ,exportTypes:['json','csv']//'json', 'xml', 'png', 'csv', 'txt', 'sql', 'doc', 'excel', 'xlsx', 'pdf'
		    ,queryParams:function(params){
		    	var parm = {};
		    	parm['pageSize'] = params.limit || 15 ;
		    	parm['pageIndex'] = (params.offset / params.limit) + 1 || 1;
		    	return parm;
		    },
		    /*
		     onRefresh:function(params){
		    	console.info(params);
		    	console.info($(_self).bootstrapTable('getData'));
		    }
		     */
		    onAll:function(name, args){
		    	if(name == 'load-success.bs.table'){
		    		//console.info(name);
		    		//debugger
			    	//console.info($(_self).bootstrapTable('getData'));
			    	var data = $(_self).bootstrapTable('getData');
			    	//console.info($(_self).bootstrapTable('getOptions'));
			    	var options = $(_self).bootstrapTable('getOptions');
			    	if(!data.length && options.totalRows > 0){
			    		$(_self).bootstrapTable('refresh');//, {silent : true,pageNumber:options.pageNumber-1}
			    	}
		    	}
		    	
		    }
		    //,singleSelect:true
		    //,showColumns:true
		    //,showRefresh:true
		    //,showToggle:true
		    //,minimumCountColumns:20
		    //,detailView:true
		    //,detailFormatter:function(index, row){
		    //	return row.id+'   '+row.name+'   '+row.price;
		    //}
	};
	
	if(options && typeof(options) == 'object'){
		$.extend(baseopt,options);
	}
	
	$(_self).bootstrapTable(baseopt);
	
}

$(function(){
	
//$("table").colResizable();

})